/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.extns.pim;

import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.Event;
import javax.microedition.pim.EventList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.RepeatRule;
import javax.microedition.pim.ToDo;
import javax.microedition.pim.ToDoList;

/**
 * used to perform PIM operations
 * 
 * @author kentelt
 * 
 */
public class PIMUtil {

	public final static int CONTACT_TYPE_HOME = 1;
	public final static int CONTACT_TYPE_WORK = 0;
	public final static int REPEAT_DAILY = 0;
	public final static int REPEAT_MONTHLY = 2;

	public final static int REPEAT_WEEKLY = 1;
	public final static int REPEAT_YEARLY = 3;

	/**
	 * Add a new contact to the address book
	 * 
	 * @param name
	 * @param number
	 * @param type
	 * @param organisation
	 * @param email
	 * @return boolean success of the operation
	 */
	public boolean createContact(String name, String number, int contactType, String organisation, String email) {
		ContactList contactList;
		try {
			contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.WRITE_ONLY);
			Contact contact = contactList.createContact();
			String[] contactElements = new String[5]; // 5 name elements
			try {
				contactElements[Contact.NAME_GIVEN] = name;
			} catch (IllegalArgumentException iae) {
				System.out.println(iae.toString());
			}
			if (contactList.isSupportedField(Contact.NAME)) {
				contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, contactElements);
			}
			switch (contactType) {
			case 0:
				if (contactList.isSupportedField(Contact.TEL)
						&& contactList.isSupportedAttribute(Contact.TEL, Contact.ATTR_HOME)) {
					contact.addString(Contact.TEL, Contact.ATTR_WORK, number);
				}
				break;
			case 1:
				if (contactList.isSupportedField(Contact.TEL)) {
					contact.addString(Contact.TEL, Contact.ATTR_HOME, number);
				}
				break;
			}

			if (contactList.isSupportedField(Contact.EMAIL)) {
				contact.addString(Contact.EMAIL, Contact.ATTR_WORK, email);
			}
			if (contactList.isSupportedField(Contact.ORG)) {
				contact.addString(Contact.ORG, Contact.ATTR_WORK, organisation);
			}
			contact.commit();
			return true;
		} catch (PIMException e) {
			return false;
		}

	}

	/*
	 * public String retrieveContact() { String runner=new String(); try{
	 * ContactList contactList =
	 * (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST,
	 * PIM.READ_WRITE); Enumeration en = contactList.items(); while
	 * (en.hasMoreElements()) { Contact c = (Contact)en.nextElement(); int[]
	 * fieldIds = c.getFields(); int id; for(int index = 0; index <
	 * fieldIds.length; ++index) { id = fieldIds[index];
	 * if(c.getPIMList().getFieldDataType(id) == Contact.STRING) { for(int j=0;
	 * j < c.countValues(id); ++j) { String value = c.getString(id, j); //
	 * custom statement runner
	 * +=c.getPIMList().getFieldLabel(id)+"="+value+"\n";
	 * System.out.println(c.getPIMList().getFieldLabel(id) + "=" + value); } } }
	 * } } catch(PIMException e) { System.out.println(e.toString()); } return
	 * runner; }
	 */

	/**
	 * creates a new entry in the calendar application
	 * 
	 * @param summary
	 * @param location
	 * @param start_date
	 * @param end_date
	 * @param note
	 * @param repeat
	 * @return boolean success of the operation
	 */
	public boolean createEvent(String summary, String location, long start_date, long end_date, String note,
			int repeatRule) {

		try {
			Event event;
			EventList eventList = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.WRITE_ONLY);
			event = eventList.createEvent();
			event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, summary);
			event.addString(Event.LOCATION, PIMItem.ATTR_NONE, location);
			event.addDate(Event.END, PIMItem.ATTR_NONE, end_date);
			event.addDate(Event.START, PIMItem.ATTR_NONE, start_date);
			event.addString(Event.NOTE, PIMItem.ATTR_NONE, note);
			event.setRepeat(setRptRule(repeatRule));
			event.commit();
			return true;
		} catch (PIMException e) {

			System.out.println(e.toString());
			return false;
		}

	}

	/**
	 * create a new task
	 * 
	 * @param summary
	 * @param note
	 * @param due_date
	 * @param priority
	 * @param stat
	 * @return boolean success of the operation
	 */
	public boolean createTask(String summary, String note, long due_date, int priority, int stat) {
		try {
			ToDoList tdLst = (ToDoList) PIM.getInstance().openPIMList(PIM.TODO_LIST, PIM.WRITE_ONLY);
			ToDo tsk = tdLst.createToDo();
			tsk.addDate(ToDo.DUE, PIMItem.ATTR_NONE, due_date);
			tsk.addString(ToDo.SUMMARY, PIMItem.ATTR_NONE, summary);
			tsk.addString(ToDo.NOTE, PIMItem.ATTR_NONE, note);
			tsk.addInt(ToDo.PRIORITY, PIMItem.ATTR_NONE, priority);
			tsk.addInt(PIMItem.EXTENDED_FIELD_MIN_VALUE + 9, PIMItem.ATTR_NONE, stat);
			tsk.commit();
			return true;
		} catch (PIMException e) {
			System.out.println(e.toString());
			return false;
		}

	}

	private RepeatRule setRptRule(int index) {
		RepeatRule rule = new RepeatRule();
		switch (index) {
		case 0:
			rule.setInt(RepeatRule.FREQUENCY, RepeatRule.DAILY);
			break;
		case 1:
			rule.setInt(RepeatRule.FREQUENCY, RepeatRule.WEEKLY);
			break;
		case 2:
			rule.setInt(RepeatRule.FREQUENCY, RepeatRule.MONTHLY);
			break;
		case 3:
			rule.setInt(RepeatRule.FREQUENCY, RepeatRule.YEARLY);
			break;
		default:
			rule.setInt(RepeatRule.FREQUENCY, RepeatRule.DAILY);
			break;
		}
		return rule;
	}
}
